package cat.joronya.supermanager.team;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import cat.joronya.supermanager.R;
import cat.joronya.supermanager.drawer.DrawerActivity;
import cat.joronya.supermanager.drawer.DrawerItem;
import cat.joronya.supermanager.home.HomeActivity;
import cat.joronya.supermanager.market.MarketListActivity;
import cat.joronya.supermanager.privateleague.PrivateLeagueListActivity;
import cat.joronya.supermanager.team.MyTeamDetailFragment.OnProgressListener;

/**
 * Activity to list the user teams.
 * @author pol
 */
public class MyTeamsListActivity extends DrawerActivity implements OnProgressListener
{
	public static final String TAG = "Supervermut";

	/** 
	 * Called when the activity is first created. 
	 */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	// set layout and call super onCreate
    	setmLayoutResource(R.layout.drawer_layout);
    	setmLayoutId(R.id.drawer_layout);
    	super.onCreate(savedInstanceState);
    	
    	// setejem el fragment q toca
        FragmentManager fragmentManager = getSupportFragmentManager();
        fragmentManager.beginTransaction()
                       .replace(R.id.fragment, new MyTeamsListFragment())
                       .commit();
        
        // titol de la pantalla home inicalment
        setTitle(getResources().getString(R.string.my_teams_tab));
    } 
    
    @Override
    protected void onNewIntent(Intent intent) 
    {
    	super.onNewIntent(intent);
    }
    
    protected DrawerItem[] getDrawerItems()
	{
		mDrawerTitles = getResources().getStringArray(R.array.drawer_titles);
		mDrawerIcons = getResources().getStringArray(R.array.drawer_icons);

		DrawerItem[] items = new DrawerItem[mDrawerTitles.length];
		for(int i=0; i<mDrawerTitles.length; i++)
		{
			int icon = getResources().getIdentifier(mDrawerIcons[i], "drawable", this.getPackageName());
			items[i] = DrawerItem.create(mDrawerTitles[i], icon);
		}

		return items;
	}

	/** 
	 * Swaps fragments in the main content view 
	 */
	protected void selectItem(int position) 
	{
		setTitle(mDrawerTitles[position]);
		Log.d(TAG,"selectItem on drawer");

		switch(position) 
		{
			case 0:
				// home fragment
				Intent homeActivity = new Intent(getBaseContext(), HomeActivity.class);
				startActivity(homeActivity);
				overridePendingTransition(0,0);
				break;
			case 1:
				// private league list activity
				Intent privateLeagueListActivity = new Intent(getBaseContext(), PrivateLeagueListActivity.class);
        		startActivity(privateLeagueListActivity);
        		overridePendingTransition(0,0);
				break;
			case 2:
				// market list activity
				Intent marketListActivity = new Intent(getBaseContext(), MarketListActivity.class);
				startActivity(marketListActivity);
				overridePendingTransition(0,0);
				break;
			case 3:
				break;
			default:

			break;
		}

		mDrawerLayout.closeDrawer(mDrawerList);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) 
	{
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	// show progress dialog with message and cancelable status
	public void showProgress(String message, boolean cancelable) 
	{
		ActionProgressDialogFragment newFragment = ActionProgressDialogFragment.newInstance(message, cancelable);
	    newFragment.show(getSupportFragmentManager(), "progress_dialog");
	}

	// dismiss progress dialog if its available
	public void dismissProgress() 
	{
		ActionProgressDialogFragment dialog = (ActionProgressDialogFragment)getSupportFragmentManager().findFragmentByTag("progress_dialog");
		if( dialog != null )
			dialog.dismissAllowingStateLoss();
	} 
}
